package com.tanhua.dubbo.api;

import com.tanhua.dubbo.api.mongo.CommentApi;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.RecommendUser;
import com.tanhua.model.vo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;
/**
 * 圈子互动服务接口实现类
 */
@DubboService
@Slf4j
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public PageResult<Comment> findPageComment(Long page, Long pagesize, String movementId) {
        return new PageResult<>();
    }

    /**
     * 圈子互动操作（评论、点赞、喜欢等）
     *
     * @param comment
     * @return
     */
    @Override
    public Integer add(Comment comment) {
        log.debug("*******comment{}*********",comment);
        //1.先根据动态id查询动态表获取动态发布者用户id
        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
        Long publishUserId = movement.getUserId();//动态发布者用户id
        comment.setPublishUserId(publishUserId);
        comment.setCreated(System.currentTimeMillis());//发布时间
        //2.保存圈子互动操作（评论、点赞、喜欢等）
        mongoTemplate.insert(comment);
        log.debug("*******comment***插入成功了******");
        //3.根据动态id更新动态表 点赞、评论、喜欢数量
        //update table set xx=+1 where xxxxx=id
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(comment.getPublishId()));//动态主键id

        Update update = new Update();
        update.inc(getCollName(comment.getCommentType()),1);//针对某个字段自增长

        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);//查询并修改后返回最新的值
        Movement mv = mongoTemplate.findAndModify(query, update, options, Movement.class);
        log.debug("***add****mv{}*********",mv);
        return mv.statisCount(comment.getCommentType());
    }



    /**
     * 圈子互动操作（取消点赞、取消喜欢等）
     *
     * @param comment
     * @return
     */
    @Override
    public Integer delete(Comment comment) {
        //1.删除动态评论表记录
        Query query = new Query();
        query.addCriteria(
                Criteria.where("publishId").is(comment.getPublishId())
                        .and("commentType").is(comment.getCommentType())
                        .and("userId").is(comment.getUserId())
        );
        mongoTemplate.remove(query,Comment.class);
        //2.更新动态发布表点赞数量 喜欢数量 -1

        Query query2 = new Query();
        query2.addCriteria(Criteria.where("id").is(comment.getPublishId()));//动态主键id

        Update update = new Update();
        update.inc(getCollName(comment.getCommentType()),-1);//针对某个字段自增长

        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);//查询并修改后返回最新的值
        Movement mv = mongoTemplate.findAndModify(query2, update, options, Movement.class);
        log.debug("***delete****mv{}*********",mv);
        return mv.statisCount(comment.getCommentType());
    }



    /**
     * 通过评论类型，获取要更新的动态表中的列名
     * @param commentType
     * @return
     */
    private String getCollName(int commentType){
        if(CommentType.LIKE.getType() == commentType){
            return "likeCount";
        }else if(CommentType.COMMENT.getType() == commentType){
            return "commentCount";
        }else{
            return "loveCount";
        }
    }
}